package rbac

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"store/logic"
	"store/models/rbac"
)

type AdminAccessLogic struct {
	logic.RestLogic
	Ctx *gin.Context
}

type AdminAccessInput struct {
	logic.XmlInput
	RoleId  uint64 `json:"roleId" xml:"roleId" form:"roleId" field:"role_id" binding:"min=0" msg:"角色id必须是非负数"`
	RightId uint64 `json:"rightId" xml:"rightId" form:"rightId" field:"right_id" binding:"min=0" msg:"权限id必须是非负数"`
	IsOn    uint8  `json:"isOn" xml:"isOn" form:"isOn" field:"is_on" binding:"min=0" msg:"是否启用(0禁用1启用)必须是非负数"`
	Id      uint64 `json:"id" xml:"id" form:"id" field:"id" binding:"min=0" msg:"必须是非负数"`
}

/**克隆logic,用于存放*gin.Context
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminAccessLogic) Clone(ctx *gin.Context) logic.RestLogic {
	newLogic := *this
	newLogic.Ctx = ctx
	return &newLogic
}

/**解析http输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminAccessLogic) LoadInput() (input interface{}, err error) {
	in := AdminAccessInput{
		IsOn: 1,
	}
	err = this.Ctx.ShouldBind(&in)
	return in, err
}

/**解析http输入转化为update输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminAccessLogic) LoadInput4Update(where map[string]interface{}) (input interface{}, version uint64, err error) {
	model, ok := this.View(where)
	if !ok {
		err = fmt.Errorf("not found")
		return
	}

	val := model.(rbac.AdminAccess)
	value := AdminAccessInfo2Input(&val)
	err = this.Ctx.ShouldBind(&value)
	input = value
	version = val.Version
	return
}

/**创建输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminAccessLogic) NewCreateInput() interface{} {
	return AdminAccessInput{
		IsOn: 1,
	}
}

/**数据库Model转Input
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminAccessInfo2Input(info *rbac.AdminAccess) AdminAccessInput {
	return AdminAccessInput{
		IsOn:    info.IsOn,
		Id:      info.Id,
		RoleId:  info.RoleId,
		RightId: info.RightId,
	}
}

/**插入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminAccessLogic) Add(input interface{}) (id uint64) {
	in := input.(*interface{})
	info := logic.Input2Map((*in).(AdminAccessInput))
	id = rbac.AdminAccessCreate(this.Ctx, info)
	if id > 0 {
		ResetAllEnableAccess(this.Ctx)
	}
	return id
}

/**删除
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminAccessLogic) Delete(id uint64) (rows int64) {
	rows = rbac.AdminAccessDelete(this.Ctx, id)
	if rows > 0 {
		ResetAllEnableAccess(this.Ctx)
	}
	return rows
}

/**修改
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminAccessLogic) Update(version uint64, input interface{}) (rows int64) {
	infoInput := input.(AdminAccessInput)
	info := logic.Input2Map(infoInput)
	rows = rbac.AdminAccessUpdate(this.Ctx, infoInput.Id, version, info)
	if rows > 0 {
		ResetAllEnableAccess(this.Ctx)
	}
	return rows
}

/**启用禁用
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminAccessLogic) UpOrDown(id uint64, value uint8) (rows int64) {
	rows = rbac.AdminAccessUpOrDown(this.Ctx, id, value)
	if rows > 0 {
		ResetAllEnableAccess(this.Ctx)
	}
	return rows
}

/**详情
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminAccessLogic) View(where map[string]interface{}) (info interface{}, exist bool) {
	model := rbac.AdminAccessInfo(this.Ctx, where)
	return model, model.Id > 0
}

/**列表分页检索
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminAccessLogic) Index(page int64, pageSize int64, where map[string]interface{}, orders []string) (pageData map[string]interface{}) {
	return rbac.AdminAccessIndex(this.Ctx, page, pageSize, where, orders)
}
